/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IJudgeInfoDAO;
import com.ssyt.examinationsystem.entity.JudgeInfo;
import com.ssyt.examinationsystem.entity.PageBean;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Long.
 * @version 1.0
 * @className JudgeInfoDAOImpl
 * @since 2016年10月30日
 */
public class JudgeInfoDAOImpl implements IJudgeInfoDAO {

    /*
     * (non-Javadoc)
     *
     * @see com.ssyt.examinationsystem.dao.IJudgeInfoDAO#addJudgeInfo(com.ssyt.
     * examinationsystem.entity.JudgeInfo)
     */
    @Override
    public int addJudgeInfo(JudgeInfo judge) {
        String sql = "insert into judgeInfo values(null,?,?,?)";
        Object[] parameters = {judge.getQuestion(), judge.getAnswer(), judge.getCourse().getCourseId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public List<Map<String, Object>> getJudge() {
        return JdbcUtils.executeQuery("select * from judgeinfo");
    }

    @Override
    public int updateJudge(JudgeInfo judge) {
        String sql = "update judgeinfo set question=?,answer=?,courseid=? where judgeid=?";
        Object[] parameters = {judge.getQuestion(), judge.getAnswer(), judge.getCourse().getCourseId(),
                judge.getJudgeId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int deleteJudge(String judgeId) {
        return JdbcUtils.executeUpdate("delete from judgeinfo where judgeid=?", judgeId);
    }

    @Override
    public List<Map<String, Object>> getJudgeByPage(PageBean page) {
        String sql = "select * from judgeinfo limit ?,?";
        // 获取每页显示记录数，当前页
        Integer pageSize = page.getPageSize();
        Integer currentPage = page.getCurrentPage();
        // 计算每页显示第一条记录索引
        Integer startIndex = (currentPage - 1) * pageSize;

        Object[] parameters = {startIndex, pageSize};
        return JdbcUtils.executeQuery(sql, parameters);
    }

    @Override
    public int getTotalCount() {
        String sql = "select * from judgeinfo";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql);
        return CollectionUtils.isEmptyForList(list) ? list.size() : 0;
    }

    @Override
    public Map<String, Object> getJudgeInfoById(String judgeId, String courseId) {
        String sql = "select * from judgeInfo where judgeId= ? and courseId = ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, judgeId, courseId);

        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<Map<String, Object>> getRandomJudgeInfoById(String number, String courseId) {
        String sql = "select * from judgeInfo where courseId=? order by rand() limit ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, courseId, Integer.parseInt(number));

        return list;
    }

}
